/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import mx.edu.itschapala.sistemas.septimo.control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.control.ProductoLNLocal;
import mx.edu.itschapala.sistemas.septimo.control.VentaLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.modelo.Venta;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVentana;

/**
 *
 * @author Estudiante
 */
public class RegistrarV extends javax.swing.JFrame {

    private ClienteLnLocal clienteControl;
    private ProductoLNLocal productoControl;
    private VentaLnLocal ventaControl;
    private TipoVentana tipoVentana;

    /**
     * Creates new form RegistrarV
     */
    public RegistrarV(ClienteLnLocal clienteControl, ProductoLNLocal productoControl, TipoVentana tipoVentana,VentaLnLocal ventaControl) {
        this.clienteControl = clienteControl;
        this.productoControl = productoControl;
        this.tipoVentana = tipoVentana;
        this.ventaControl=ventaControl;
        initComponents();
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                title.setText("Registrar Venta");
                jTextNameC.setEditable(false);
                jTextNameP.setEditable(false);
                break;
            case VENTANA_BUSCAR:
                title.setText("Buscar por ID");
                jBoGuardar.setText("Acaptar");
                jBoCancelar.setVisible(false);
                jTextIdC.setEditable(false);
                jTextCodeP.setEditable(false);
                jTextNameC.setEditable(false);
                jTextNameP.setEditable(false);
                jTextDate.setEditable(false);
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        title = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jBoGuardar = new javax.swing.JButton();
        jBoCancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextIdV = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextIdC = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextNameC = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextCodeP = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextNameP = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextDate = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 436, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );

        title.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        title.setText("Registro de Venta");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addComponent(title)
                .addContainerGap(79, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(title)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jBoGuardar.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jBoGuardar.setText("Guardar");
        jBoGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBoGuardarActionPerformed(evt);
            }
        });

        jBoCancelar.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jBoCancelar.setText("Cancelar");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jBoGuardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBoCancelar)
                .addGap(29, 29, 29))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBoGuardar)
                    .addComponent(jBoCancelar))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jPanel2.setLayout(new java.awt.GridLayout(6, 2, -130, 5));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel1.setText("ID Venta");
        jPanel2.add(jLabel1);

        jTextIdV.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextIdVKeyPressed(evt);
            }
        });
        jPanel2.add(jTextIdV);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel2.setText("Cliente ID");
        jPanel2.add(jLabel2);
        jPanel2.add(jTextIdC);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel6.setText("Nombre");
        jPanel2.add(jLabel6);
        jPanel2.add(jTextNameC);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel3.setText("Codigo Producto");
        jPanel2.add(jLabel3);
        jPanel2.add(jTextCodeP);

        jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel7.setText("Nombre Producto");
        jPanel2.add(jLabel7);
        jPanel2.add(jTextNameP);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel4.setText("Fecha");
        jPanel2.add(jLabel4);
        jPanel2.add(jTextDate);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 406, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBoGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBoGuardarActionPerformed
        switch (tipoVentana) {

            case VENTANA_REGISTRO:
                Cliente c = clienteControl.buscarPorCodigo(Integer.parseInt(jTextIdC.getText()));
                Producto p = productoControl.buscarPorCodigo(Integer.parseInt(jTextCodeP.getText()));
                if (c != null) {
                    jTextNameC.setText(c.getNombre());
                
                if (p != null) {
                    jTextNameP.setText(p.getNombre());
                    Venta v = new Venta();
                    v.setCliente(c.getId());
                    v.setId(Integer.parseInt(jTextIdV.getText()));
                    v.setProducto(p.getCodigo());
                    v.setFecha(jTextDate.getText());
                    ventaControl.registrar(v);
                    
                    JOptionPane.showMessageDialog(null, "Guardado correctamente");
                    vaciarCampos();
                }}
                
                break;
            case VENTANA_BUSCAR:
                this.dispose();
                break;

        }
    }//GEN-LAST:event_jBoGuardarActionPerformed

    private void jTextIdVKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextIdVKeyPressed
       if(tipoVentana!=tipoVentana.VENTANA_REGISTRO){
           if(evt.getKeyCode()==KeyEvent.VK_ENTER){
               Venta v=ventaControl.buscarPorCodigo(Integer.parseInt(jTextIdV.getText()));
               if(v!=null){
                   jTextIdC.setText(String.valueOf(v.getCliente()));
                   Cliente c=clienteControl.buscarPorCodigo(v.getCliente());
                   jTextNameC.setText(c.getNombre());
                   jTextCodeP.setText(String.valueOf(v.getProducto()));
                   Producto p=productoControl.buscarPorCodigo(v.getProducto());
                   jTextNameP.setText(p.getNombre());
                   jTextDate.setText(v.getFecha());
                   
//                   if(tipoVentana==TipoVentana.VENTANA_MODIFICAR){
//                       textcNombre.setEditable(true);
//                       textcTelefono.setEditable(true);
//                       textcDireccion.setEditable(true);
//                       textcSexo.setEditable(true);
//                   }
//                   if(tipoVentana==TipoVentana.VENTANA_ELIMINAR){
//                       textcNombre.setText(p.getNombre());
//                       textcTelefono.setText(String.valueOf(p.getTelefono()));
//                       textcDireccion.setText(p.getDireccion());
//                       textcSexo.setText(p.getSexo()+"");
//                       
//                       
//                   }
               }
           }
       }
    }//GEN-LAST:event_jTextIdVKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBoCancelar;
    private javax.swing.JButton jBoGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextField jTextCodeP;
    private javax.swing.JTextField jTextDate;
    private javax.swing.JTextField jTextIdC;
    private javax.swing.JTextField jTextIdV;
    private javax.swing.JTextField jTextNameC;
    private javax.swing.JTextField jTextNameP;
    private javax.swing.JLabel title;
    // End of variables declaration//GEN-END:variables

    private void vaciarCampos() {
        jTextIdV.setText("");
        jTextIdC.setText("");
        jTextCodeP.setText("");
        jTextNameC.setText("");
        jTextNameP.setText("");
        jTextDate.setText("");
    }
}
